package com.fh.controller;

import com.fh.common.ResultData;
import com.fh.entity.po.User;
import com.fh.entity.vo.UserParam;
import com.fh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("userIsExist")
    @ResponseBody
    public ResultData userIsExist(String name){
        if (StringUtils.isEmpty(name)){
            throw new RuntimeException("参数异常");
        }
        return ResultData.success(userService.userIsExist(name));
    }

    @RequestMapping("register")
    @ResponseBody
    public ResultData register(User user){
        if (StringUtils.isEmpty(user.getName())||StringUtils.isEmpty(user.getPassword())||StringUtils.isEmpty(user.getEmail())){
            throw new RuntimeException("参数异常");
        }
        userService.register(user);
        return ResultData.success("");
    }

    @RequestMapping("updatePassword")
    @ResponseBody
    public ResultData updatePassword(User user){
        if (StringUtils.isEmpty(user.getName())||StringUtils.isEmpty(user.getPassword())){
            throw new RuntimeException("处理失败，参数异常");
        }
        return ResultData.success(userService.updatePassword(user));
    }

    @RequestMapping("queryUserPageData")
    @ResponseBody
    public ResultData queryUserPageData(UserParam param){
        if (param.getPage()==null||param.getLimit()==null){
            throw new RuntimeException("请求异常，参数有误");
        }
        return ResultData.success(userService.queryUserPageData(param));
    }

    @RequestMapping("updateUserInfo")
    @ResponseBody
    public ResultData updateUserInfo(User user){
        if (user.getId()==null){
            throw new RuntimeException("请求异常，参数有误");
        }
        userService.updateUserInfo(user);
        return ResultData.success("");
    }
}
